<?php
include_once "function_xml_atom.php";
include_once "function_net_misc.php";
function GetOnlyOneValue($query_string)
{
        $results = GetAllNodeValue($query_string);
	$attributes = GetAttribute($query_string);
	
	if ($attributes->item(0)->getAttribute('country') == 'cn')
		return base64_decode($results->item(0)->nodeValue);
       	else
		return $results->item(0)->nodeValue;
}

function GetAllValueToArray($query_string)
{
	$number = GetNumberOfNode($query_string);
	$results = GetAllNodeValue($query_string);
	$i = 0;
	foreach ($results as $result)
	{
		$ret_array[$i] = $result->nodeValue;
		$i ++;
	}
	return $ret_array;
}


#$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE";
function FindAttribute($query_string, $attribute, $value)
{
        $number = GetNumberOfNode($query_string);
	
	for ($i = 1; $i < $number; $i ++)
	{
		$query_string_real = $query_string. "[". $i. "]";
		$result = GetAttribute($query_string_real)->item(0);
		if ($value == $result->getAttribute($attribute))
		{
			return $i;
		}
	}
	return 0;
}
#print FindAttribute($query_string, 'Name', 'eth8');

function AppendAllAttrOfNode($query_string, $node_name, $Valu_array)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results = $xpath->evaluate($query_string);
        $result = $results->item(0);

        $new_node = $doc->createElement($node_name);
	
	$Attr_array = array_keys($Valu_array);

	foreach ($Attr_array as $Attr)
 	{	
        	$new_node->setAttribute($Attr, $Valu_array[$Attr]);
	}
        $result->appendChild($new_node);

        $doc->save(XMLFILE);
}
 
function EditAllAttrOfNode($query_string, $Valu_array)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results = $xpath->query($query_string);
	$result = $results->item(0);	

	$Attr_array = array_keys($Valu_array);
	foreach ($Attr_array as $Attr)
        {	
                $result->setAttribute($Attr, $Valu_array[$Attr]);
        }
        $doc->save(XMLFILE);	
}

function DelSpecialNode($query_string)
{
	DeleteNode($query_string, NULL);
}

function DelNodeOnlyValue($query_string, $value)
{
	DeleteNode($query_string, $value);
}

function MoveFrontOneNode($query_string_from, $query_string_to)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results_from = $xpath->query($query_string_from);
        $result_from = $results_from->item(0);
        
	$results_to= $xpath->query($query_string_to);
        $result_to= $results_to->item(0);

        $result_to->parentNode->insertBefore($result_from,$result_to);

        $doc->save(XMLFILE);

}
#$query_string_from = "/MINI/NETWORK/ROUTES/POLICY/ROUTE[1]";
#$query_string_to = "/MINI/NETWORK/ROUTES/POLICY/ROUTE[4]";
#MoveFrontOneNode($query_string_from, $query_string_to);

function MoveEndOneNode($query_string_from, $query_string_to)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results_from = $xpath->query($query_string_from);
        $result_from = $results_from->item(0);
        
	$results_to= $xpath->query($query_string_to);
        $result_to= $results_to->item(0);

        $result_to->parentNode->insertBefore($result_from);

        $doc->save(XMLFILE);
}

function InsertOneNode($query_string_from, $node_name, $Valu_array)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results_from = $xpath->query($query_string_from);
	#print $query_string_from;
	
        $result_from = $results_from->item(0);
	#print $result_from->getAttribute('Name');

        $new_node = $doc->createElement($node_name);
        $Attr_array = array_keys($Valu_array);
        foreach ($Attr_array as $Attr)
        {       
                $new_node->setAttribute($Attr, $Valu_array[$Attr]);
        } 

	$result_from->parentNode->insertBefore($new_node, $result_from); 
        $doc->save(XMLFILE);
}

function GetHasChildNodesAttr($query_string, $attr)
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='eth0']";

}

function GetAttributList($query_string, $in_array)
{
        $key_array = $in_array;
	$ret_array = array ();
	$results = GetAttribute($query_string);
        $i = 0;
        foreach ($results as $result)
        {
		foreach ($key_array as $key)
		{
			$ret_array[$i][$key] = $result->getAttribute($key);
		}
		$i ++;
	}
	return $ret_array;

}

function GetObjectListByNameValue($name)
{
	$query_string = "//OBJECT[@Name='". $name. "']";
	$ret_list = GetAttribute($query_string);
	return $ret_list;
}
function GetGroupListByNameValue($name)
{
	$query_string = "//GROUP[@Name='". $name. "']";
	$ret_list = GetAttribute($query_string);
	return $ret_list;
}

function GetListByNameValue($name)
{
	$ret_list = array("Object" => GetObjectListByNameValue($name), "Group" => GetGroupListByNameValue($name));
	return $ret_list;
}

?>
